﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccessController
{
    public partial class PermissionDaoController
    {
        public Collection<Permission> SelectAll()
        {
            Collection<Permission> items = dao.SelectAll();

            CacheProvider.Instance.Clear<Permission>();
            foreach (var item in items) CacheProvider.Instance.Add<Permission>(item.Id, item);

            return items;
        }

        public Permission SelectByName(string permissionName)
        {
            Permission permission = null;
            Dictionary<Guid, object> permissions = CacheProvider.Instance.Get<Permission>();

            foreach (object obj in permissions.Values)
            {
                Permission item = obj as Permission;

                if (item != null && item.PermissionName == permissionName)
                {
                    permission = item;
                    break;
                }
            }

            if (permission == null)
            {
                permission = dao.SelectByName(permissionName);

                if (permission != null) CacheProvider.Instance.Add<Permission>(permission.Id, permission);
            }

            return permission;
        }
    }
}
